package de.ifgi;

import android.os.Parcel;
import android.os.Parcelable;

public class Point implements Parcelable {
	
	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

		@Override
		public Point createFromParcel(Parcel in) {
			return new Point(in);
		}

		@Override
		public Point[] newArray(int size) {
			return new Point[size];
		}
	};
	
	private double x;
	private double y;
	
	public Point() {
		super();
	}
	
	public Point(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	public Point(Parcel in) {
		readFromParcel(in);
	}

	public double getX() {
		return x;
	}
	
	public void setX(double x) {
		this.x = x;
	}
	
	public double getY() {
		return y;
	}
	
	public void setY(double y) {
		this.y = y;
	}
	
	/**
	 * Returns the distance from this Point to a specified Point.
	 * @param p The specified point.
	 * @return The distance.
	 */
	public double distance(Point p) {
		double d = Math.sqrt(Math.pow((this.x-p.getX()), 2)+Math.pow((this.y-p.getY()), 2));
		return d;
	}
	
	@Override
	public int describeContents() {
		return 0;
	}

	private void readFromParcel(Parcel in) {
		this.setX(in.readDouble());
		this.setY(in.readDouble());
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeDouble(this.getX());
		dest.writeDouble(this.getY());
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(x);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(y);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Point other = (Point) obj;
		if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
			return false;
		if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
			return false;
		return true;
	}
}
